const api = require('../../../api/mop_api');
import { toast, log, err, back } from '../../../utils/wxapi';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    markNo: '',
    name: '',
    id: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    if (options.id) {
      const { id } = options;
      this.setData({ id });
      try {
        const res = await api.board_get({ id });
        if (res.respCode === 200) {
          const item = JSON.parse(res.source);
          log(item);
          this.setData({
            markNo: item.markNo,
            name: item.name,
          });
        } else {
          toast(res.msg);
        }
      } catch (error) {
        err(error);
        throw error;
      }
    }
  },
  async post() {
    const { markNo, name } = this.data;
    try {
      const res = await api.board_post({ markNo, name });
      if (res.respCode === 200) {
        toast('创建成功');
        back();
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async put() {
    const { markNo, name, id } = this.data;
    try {
      const res = await api.board_put({ markNo, name, id });
      if (res.respCode === 200) {
        toast('修改成功');
        back();
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
});
